package com.shuoyue.ycgk.ui.account;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.utils.EditCheckUtil;
import com.shuoyue.ycgk.utils.XToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegistActivity extends BaseMvpActivity<AccountContract.Presenter>
        implements AccountContract.View, SmsContract.View {


    SmsContract.Presenter smsPresenter;
    TimeCount time;
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.phone_num)
    EditText phoneNum;
    @BindView(R.id.pass_code)
    EditText passCode;
    @BindView(R.id.get_code)
    Button getCode;
    @BindView(R.id.password1)
    EditText password1;
    @BindView(R.id.password2)
    EditText password2;
    @BindView(R.id.check_judge)
    CheckBox checkJudge;
    @BindView(R.id.judge)
    TextView judge;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_regist;
    }


    @Override
    protected void initView() {
        pageTitle.setText("用户注册");
        time = new TimeCount(60000, 1000);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new AccountContract.Presenter();
        mPresenter.attachView(this);
        smsPresenter = new SmsContract.Presenter();
        smsPresenter.attachView(this);
    }

    @Override
    public void loginSuc(UserInfo userInfo) {

    }

    @Override
    public void registSuc() {
        finish();
    }

    @Override
    public void registFail(String msg) {
        toast(msg);

    }

    @Override
    public void resetSuc() {

    }

    @Override
    public void bindPhone(String openId, String accessToken) {

    }

    @OnClick({R.id.back, R.id.get_code, R.id.judge, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.get_code:
                getVCode();
                break;
            case R.id.judge:
                break;
            case R.id.submit:
                checkInput();
                break;
        }
    }

    @Override
    public void sendSuc(String pone) {
        toast("验证码发送成功");
        time.start();
    }

    void checkInput() {
        String phoneStr = phoneNum.getText().toString();
        String pwd1 = password1.getText().toString();
        String passwordStr2 = password2.getText().toString();
        String codeStr = passCode.getText().toString();
        if (!EditCheckUtil.isTelPhoneNumber(phoneStr)) {
            XToast.toast(mContext, "请输入正确的手机号码");
            return;
        }
        if (pwd1.length() < 6) {
            XToast.toast(mContext, "请输入6-12位的密码");
            return;
        }
        if (!pwd1.equals(passwordStr2)) {
            XToast.toast(mContext, "两次密码不一致");
            return;
        }
        if (codeStr.length() == 0) {
            XToast.toast(mContext, "请输入验证码");
            return;
        }
        if (!checkJudge.isChecked()) {
            XToast.toast(mContext, "请阅读并同意用户协议");
            return;
        }
        if (pwd1.length() < 8) {
            toast("密码长度需大于等于8位");
            return;
        }
        if (!pwd1.matches("^(?:(?=.*[0-9].*)(?=.*[A-Za-z].*)(?=.*[\\W].*))[\\W0-9A-Za-z]{8,16}$")) {
            toast("必须包含数字、英文字母、特殊符号");
            return;
        }

        mPresenter.regist(phoneStr, pwd1, codeStr);
    }

    void getVCode() {
        String phone = phoneNum.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            toast("请输入手机号码");
            return;
        }
        smsPresenter.sendSms(phone);
    }


    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getCode.setBackgroundResource(R.drawable.bg_corner8_orange_stroc1);
            getCode.setTextColor(ContextCompat.getColor(mContext, R.color.themeColor));
            getCode.setEnabled(false);
            getCode.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
            getCode.setBackgroundResource(R.drawable.bg_corner8_orange_stroc1);
            getCode.setTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
            getCode.setEnabled(true);
            getCode.setText("重新获取");
        }
    }
}
